/**
 * Copyright (c) 2017-2018,Retire 吴益峰 (372310383@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.jplus.utils;

import com.jfinal.kit.HashKit;
import io.jboot.utils.StrUtil;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Map;
import java.util.UUID;

public class EncryptUtils extends HashKit {

    public static String salt() {
        int random = (int) (10 + (Math.random() * 10));
        return UUID.randomUUID().toString().replace("-", "").substring(random);// 随机长度
    }

    public static String encryptPassword(String password, String salt) {
        return sha256(password + salt);
    }

    public static boolean verlifyUser(String userPassword, String userSalt, String password) {

        if (userPassword == null)
            return false;

        if (userSalt == null) {
            return false;
        }
        return userPassword.equals(encryptPassword(password, userSalt));
    }

    public static String generateUcode(BigInteger id, String salt) {
        return md5(id + salt);
    }

    public static String signForRequest(Map<String, String> params, String secret) {
        String[] keys = params.keySet().toArray(new String[0]);
        Arrays.sort(keys);

        StringBuilder query = new StringBuilder();
        query.append(secret);
        for (String key : keys) {
            String value = params.get(key);
            if (StrUtil.areNotEmpty(key, value)) {
                query.append(key).append(value);
            }
        }
        query.append(secret);
        return HashKit.md5(query.toString()).toUpperCase();
    }

    public static void main(String[] args) {
        String salt = EncryptUtils.salt();
        System.out.println("salt=" + salt);
        System.out.println("encryptPassword=" + encryptPassword("111111", salt));
    }

}
